//
// Created by swx on 23-12-21.
//
#include <mprpcchannel.h>
#include <iostream>
#include <string>
#include "rpcExample/friend.pb.h"
#include <map>
#include <vector>
#include "rpcprovider.h"

class FriendService : public fixbug::FiendServiceRpc {
public:
    // 模拟一个好友列表
    static std::map<uint32_t, std::vector<std::string>> userFriendsMap;

    std::vector<std::string> GetFriendsList(uint32_t userid) {
        std::cout << "local do GetFriendsList service! userid:" << userid << std::endl;
        return userFriendsMap[userid % 10]; // 取模以防止越界
    }

    // 重写基类方法
    void GetFriendsList(::google::protobuf::RpcController* controller, const ::fixbug::GetFriendsListRequest* request,
        ::fixbug::GetFriendsListResponse* response, ::google::protobuf::Closure* done) {
        uint32_t userid = request->userid();
        std::vector<std::string> friendsList = GetFriendsList(userid);
        response->mutable_result()->set_errcode(0);
        response->mutable_result()->set_errmsg("");
        for (std::string& name : friendsList) {
            std::string* p = response->add_friends();
            *p = name;
        }
        done->Run();
    }
};

std::map<uint32_t, std::vector<std::string>> FriendService::userFriendsMap = {
    {0, {"Eve", "Frank", "Liam", "Olivia"}},
    {1, {"Noah", "Emma", "Oliver", "Charlotte", "Elijah", "Amelia"}},
    {2, {"James", "Ava", "Benjamin", "Sophia", "Lucas"}},
    {3, {"Isabella", "Mason", "Mia", "Ethan", "Evelyn"}},
    {4, {"Alexander", "Harper", "Henry", "Luna", "Michael"}},
    {5, {"Gianna", "Daniel", "Ellie", "Logan", "Lily"}},
    {6, {"Jackson", "Aria", "Sebastian", "Chloe"}},
    {7, {"Jack", "Penelope", "Owen", "Layla", "Samuel", "Nora"}},
    {8, {"Matthew", "Hazel", "Joseph", "Zoey", "Levi", "Riley", "Mateo"}},
    {9, {"Victoria", "David", "Stella"}}
};


int main(int argc, char** argv) {
    std::string ip = "127.0.0.1";
    short port = 7788;
    auto stub = new fixbug::FiendServiceRpc_Stub(new MprpcChannel(ip, port, false));
    // provider是一个rpc网络服务对象。把UserService对象发布到rpc节点上
    RpcProvider provider;
    provider.NotifyService(new FriendService());

    // 启动一个rpc服务发布节点 Run以后，进程进入阻塞状态，等待远程的rpc调用请求
    provider.Run(1, 7788);

    return 0;
}
